package com.example.xutilsdemo

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.xutilsdemo.db.DevicesInfoBean
import com.example.xutilsdemo.db.ListDevices
import com.example.xutilsdemo.db.UserInfoDBManager
import com.example.xutilsdemo.db.UserInfoDataBaseBean
import com.example.xutilsdemo.utils.FireGsonUtil

class MainActivity : AppCompatActivity() {
    private val userInfo: UserInfoDataBaseBean by lazy { UserInfoDataBaseBean() }
    private lateinit var devicesList: ListDevices<DevicesInfoBean>
    private val devicesInfoBean by lazy { DevicesInfoBean() }
    val thread: Thread by lazy { Thread() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        UserInfoDBManager.instance.initDB()
        initData()
    }

    private fun initData() {
        devicesList = ListDevices()
        // userInfo = UserInfoDataBaseBean("1","张帅","18","4","100","", devicesList!!)
    }

    fun add(view: View) {
        try {
            devicesInfoBean.deviceName = "客厅空调"
            devicesInfoBean.deviceType = "AIC"
            devicesInfoBean.status = "on"
            devicesInfoBean.address = "01"
            devicesInfoBean.values = "10"
            devicesList.add(devicesInfoBean)
            userInfo.let {
                it.id = "1"
                it.name = "张帅"
                it.age = "18"
                it.account = "4"
                it.size = "100"
                it.avatar = ""
                it.devices = devicesList
                UserInfoDBManager.instance.saveUserInfo(it)
                ToastUtils.showShort("添加数据成功" + FireGsonUtil.objectToJson(it))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun delete(view: View) = try {
        thread.run {
            UserInfoDBManager.instance.deleteAllUsers()
        }
        LogUtils.d("=删除后的用户信息===" + UserInfoDBManager.instance.getAllUserInfo())
    } catch (e: Exception) {
        e.printStackTrace()
    }


    fun update(view: View) {
        try {
            devicesList.clear()
            devicesInfoBean.deviceName = "卧室空调"
            devicesInfoBean.deviceType = "AIC"
            devicesInfoBean.status = "off"
            devicesInfoBean.address = "02"
            devicesInfoBean.values = "06"
            devicesList.add(devicesInfoBean)
            userInfo.run {
                this.id = "22"
                this.name = "李四"
                this.size = "15"
                this.account = "12345"
                this.age = "18"
                this.avatar = ""
                this.devices = devicesList!!
                UserInfoDBManager.instance.updateUserInfo(userInfo)
                LogUtils.d("=更新用户信息===" + FireGsonUtil.objectToJson(userInfo))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun query(view: View) {
        try {
            try {
                val userInfo: UserInfoDataBaseBean? =
                        UserInfoDBManager.instance.queryUserInfoByName("张帅")
                LogUtils.d("=查询用户信息===" + FireGsonUtil.objectToJson(userInfo))
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun queryAll(view: View) {
        try {
            val userList: List<UserInfoDataBaseBean>? = UserInfoDBManager.instance.getAllUserInfo()
            LogUtils.d("=查询所有用户信息===" + FireGsonUtil.listToJson(userList))
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

